#include "DHD_AnimationEngine.h"
#include "../Wrappers/CSGD_TextureManager.h"


void cAnimationE::Update(float _fDt)
{


}

void cAnimationE::Render( int _nPosX, int _nPosY, bool _bIsXFlipped, float _fScale, DWORD _dwcolor)
{
	
}



bool cAnimationE::Load (const char*  sXmlFileName)
{
	TiXmlDocument doc;

	if(doc.LoadFile(sXmlFileName) ==false)
		return false;

	TiXmlElement* pRoot = doc.RootElement();

	if(!pRoot)
		return false;

	TiXmlElement* pAnimation = pRoot->FirstChildElement("Animation");

	if(pAnimation)
	{
		while(pAnimation)
		{
			tAnimation tmp;

			if(pAnimation->Attribute("Name"))
				tmp._name = pAnimation->Attribute("Name");
			if(pAnimation->Attribute("Looping"))
				tmp._IsLooping = pAnimation->Attribute("Looping");

			TiXmlElement* pFrame = pAnimation->FirstChildElement("Frame");

			if(pFrame)
			{
				while(pFrame)
				{
					tFrame tmpF;
					if(pFrame->Attribute("Number"))
						tmpF.nNumber = (int)pFrame->Attribute("Number");
					if(pFrame->Attribute("Timer"))
						tmpF.fTimer = (int)pFrame->Attribute("Timer");
					if(pFrame->Attribute("Event"))
						tmpF.sEvent = pFrame->Attribute("Event");

					if(pFrame->FirstChildElement("RenderRect"))
					{
						tmpF.rRenderRect.left = (long)pFrame->FirstChildElement("RenderRect")->Attribute("X");
						tmpF.rRenderRect.top = (long)pFrame->FirstChildElement("RenderRect")->Attribute("Y");
						tmpF.rRenderRect.right = (long)pFrame->FirstChildElement("RenderRect")->Attribute("Width");
						tmpF.rRenderRect.bottom = (long)pFrame->FirstChildElement("RenderRect")->Attribute("Height");
					}

					if(pFrame->NextSiblingElement("CollisionRect"))
					{
						tmpF.rCollisionRect.left = (long)pFrame->NextSiblingElement("CollisionRect")->Attribute("X");
						tmpF.rCollisionRect.top = (long)pFrame->NextSiblingElement("CollisionRect")->Attribute("Y");
						tmpF.rCollisionRect.right = (long)pFrame->NextSiblingElement("CollisionRect")->Attribute("Width");
						tmpF.rCollisionRect.bottom = (long)pFrame->NextSiblingElement("CollisionRect")->Attribute("Height");
					}

					if(pFrame->NextSiblingElement("ActiveRect"))
					{
						tmpF.rActiveRect.left = (long)pFrame->NextSiblingElement("ActiveRect")->Attribute("X");
						tmpF.rActiveRect.top = (long)pFrame->NextSiblingElement("ActiveRect")->Attribute("Y");
						tmpF.rActiveRect.right = (long)pFrame->NextSiblingElement("ActiveRect")->Attribute("Width");
						tmpF.rActiveRect.bottom = (long)pFrame->NextSiblingElement("ActiveRect")->Attribute("Height");
					}

					if(pFrame->NextSiblingElement("AnchorPoint"))
					{
						tmpF.pAnchorPoint.x = (long)pFrame->NextSiblingElement("AnchorPoint")->Attribute("X");
						tmpF.pAnchorPoint.y = (long)pFrame->NextSiblingElement("AnchorPoint")->Attribute("Y");
					}

					tmp._vFrames.push_back(tmpF);
					pFrame = pFrame->NextSiblingElement("Frame");

				}
			}
			m_vAnimations.push_back(tmp);
			pAnimation = pAnimation->NextSiblingElement("Animation");
		}
	}
	return true;
}